const delay = require('./delay');
const retry = ( (fn,n,d,blankFn)=>{
    return new Promise(async (resolve,reject)=>{
        for(let i = 0;i<n;i++){
            try{
                if(i>0){
                   await delay(d?parseInt(d):1000);
                   if(blankFn){
                    await blankFn();
                   }
                }
                await fn();
                resolve();
                break;
            }
            catch(error){
                if(i<n-1){
                    continue;
                }
                reject(error);
                return;
            }
        }
    });
});

module.exports = retry;